package com.ysqcould.web.controller.content;


import com.alibaba.fastjson.JSONObject;
import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.content.domain.PressType;
import com.ysqcould.content.domain.SecondType;
import com.ysqcould.content.mapper.SecondTypeMapper;
import com.ysqcould.content.service.IPressTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

import static com.ysqcould.allocation.utils.MultipartFileUtils.getFilePath;

/**
 * 所属类型Controller
 * 
 * @author ruoyi
 * @date 2022-06-29
 */
@RestController
@RequestMapping("/content/type")
public class PressTypeController extends BaseController
{
    @Autowired
    private IPressTypeService pressTypeService;

    @Resource
    private SecondTypeMapper secondTypeMapper;

    /**
     * 查询所属类型列表
     */
    @PreAuthorize("@ss.hasPermi('content:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(PressType pressType)
    {
        startPage();
        List<PressType> list = pressTypeService.selectPressTypeList(pressType);
       list.forEach(p->{
            final List<SecondType> types = secondTypeMapper.selectSecondTypeByIdList(p.getId());
            p.setSecondTypes(types);
        });
        return getDataTable(list);
    }

    /**
     * 导出所属类型列表
     */
    @PreAuthorize("@ss.hasPermi('content:type:export')")
    @Log(title = "所属类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PressType pressType)
    {
        List<PressType> list = pressTypeService.selectPressTypeList(pressType);
        ExcelUtil<PressType> util = new ExcelUtil<PressType>(PressType.class);
        util.exportExcel(response, list, "所属类型数据");
    }

    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "文章一级类型图标上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        final JSONObject path = getFilePath(file);
        final String pictures = UploadFileUtil.AliOSSUpload((InputStream) path.get("in"), (String)path.get("name"));
        return AjaxResult.success("上传成功",pictures);
    }

    @Log(title = "新增二级分类数据" ,businessType = BusinessType.INSERT)
    @PostMapping("/addSecond")
    public AjaxResult addSecond(@RequestBody SecondType type){

        System.out.println(type);
        return toAjax(secondTypeMapper.insertSecondType(type)) ;
    }

    /**
     * 删除所属类型
     */
    @PreAuthorize("@ss.hasPermi('content:type:remove')")
    @Log(title = "所属类型", businessType = BusinessType.DELETE)
    @PostMapping("/{id}")
    public AjaxResult removeSecondType(@PathVariable("id") Long id)
    {
        final SecondType type = secondTypeMapper.selectSecondTypeById(id);
        type.setStatus(2L);
        return toAjax(secondTypeMapper.updateSecondType(type));
    }
    /**
     * 获取所属类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pressTypeService.selectPressTypeById(id));
    }

    /**
     * 新增所属类型
     */
    @PreAuthorize("@ss.hasPermi('content:type:add')")
    @Log(title = "所属类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PressType pressType)
    {
        pressType.setAsId(getUserId());

        return toAjax(pressTypeService.insertPressType(pressType));
    }

    /**
     * 修改所属类型
     */
    @PreAuthorize("@ss.hasPermi('content:type:edit')")
    @Log(title = "所属类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PressType pressType)
    {
        return toAjax(pressTypeService.updatePressType(pressType));
    }

    /**
     * 删除所属类型
     */
    @PreAuthorize("@ss.hasPermi('content:type:remove')")
    @Log(title = "所属类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pressTypeService.deletePressTypeByIds(ids));
    }
}
